$(function () {
    $('body').on("click",".delete",function(){
      var  that =$(this);
        var id =  $(this).data("id");
            $.ajax({
                url: baseloaction + "/mobile/uc/userInfo/deleteUserAddressById",
                data: {"id": id},
                type: "post",
                dataType: "json",
                success: function (result) {
                    Zepto.toast(result.message);
                    if(result.success){
                        $(that).parents('.card').remove();
                    }
                },
                error: function (error) {
                    Zepto.toast("系统繁忙,请稍后重试");
                }
            })

    }).on("click",".editor",function(){
        var id =  $(this).data("id");
        window.location.href= baseloaction + "/mobile/uc/userInfo/addUserAddress?id="+id;

    });



var statusList =$(".checkbox-status");

if(statusList!=undefined&&statusList.length>0){
    statusList.each(function (index,em) {
        statusChange(em);
    });
}

function statusChange(em){
    $(em).on('change',function () {
        var em_index=$(this).data("index");
        statusList.each(function (index,em) {
            var em_index_to=$(em).data("index");
            if(em_index!=em_index_to){
                $(em).prop("checked",false);
            }
        });
        var id =  $(this).data("id");
        var status = 2;
        if( $(this).attr("checked")==true){
            status=1;
        }
        if(status==2){
            $(this).prop("checked",true);
            Zepto.toast("必须设置一个默认地址");
            return
        }

        setUserAddressDefault(id,status);
    });
}


    function setUserAddressDefault(id,status){
        $.ajax({
            url: baseloaction + "/mobile/uc/userInfo/setUserAddressDefault",
            data: {"id": id,"status":status},
            type: "post",
            dataType: "json",
            success: function (result) {
                Zepto.toast(result.message);
            },
            error: function (error) {
                Zepto.toast("系统繁忙,请稍后重试");
            }
        })
    }

});



function goBack(){
    var  noMobile  = getCookie("noMobile");
    if(noMobile!=null){
        if (noMobile == "1" ){
            android.goBack();
        }
        if( noMobile == "2") {
            window.webkit.messageHandlers.goBack.postMessage("");
        }
        return true;
    }
    var goBacUrl =  sessionStorage.goBacUrl
    if(goBacUrl==''||goBacUrl==null||goBacUrl==undefined){
        window.location.href=baseloaction+ "/mobile/uc/userInfo";
        return
    }
    sessionStorage.goBacUrl="";
    window.location.href=goBacUrl;

}



